package com.zyb.framework.generator;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 描述:
 *
 * @author z3412
 * @create 2019-08-13 22:52
 */
@Aspect
@Component
@Configuration
public class GeneratorKeyInterceptor {
    @Bean
     public IdWorker idWorker(){
        IdWorker idWorker = new  IdWorker();
        return idWorker;

    }


    //定义一个空方法 借用其注解抽取切点表达式
    @Pointcut("execution (public * com.baomidou.mybatisplus.core.toolkit.IdWorker.get32UUID())")
    //@Pointcut("execution (public * SysUserController.test())")
    public void point() {}

    @Before("point()")
    public void before(JoinPoint joinPoint) throws Exception{
        System.out.println("进入aop-----------------------");
    }
    @After("point()")
    public void after(JoinPoint joinPoint){
        for (int i = 0; i < joinPoint.getArgs().length; i++) {
            System.out.println(joinPoint.getArgs()[i]);
        }
        System.out.println(joinPoint.getSignature().getName());

        System.out.println("=====checkSecurity====");

    }

   // @AfterReturning(returning="rvt", pointcut="@annotation(com.example.tuomin.annotations.TuominAnn)")
    /*public Object AfterExec(JoinPoint joinPoint,Object rvt){
        System.out.println(rvt);
        return "---"+rvt;

    }*/

    @Around("point()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("beginning----");
        Object object = pjp.proceed();    //运行doSth()，返回值用一个Object类型来接收
        //object = "Mission Two";   //改变返回值
        object = UUID.randomUUID().toString();
        System.out.println("ending----"+object);
        return object;
    }
}
